package com.hexacta.hrs.test.data;

import static com.hexacta.hrs.test.data.DomainTestDataDependencyCreator.HEXACTA_DOMAIN;
import static com.hexacta.hrs.test.data.LocaleTestDataDependencyCreator.SPANISH_LOCALE;

import java.util.List;
import java.util.Map;

import org.springframework.orm.hibernate3.HibernateTemplate;
import org.vectrics.domain.Domain;
import org.vectrics.location.Locale;
import org.vectrics.user.Person;
import org.vectrics.user.UserPrivilege;
import org.vectrics.user.UserPrivilegeType;

/**
 * Test data dependency creator for the Person class.
 * <p>
 * TODO We should review this if we want more meaningful data.
 * 
 * @author tpollak
 */
public class PersonTestDataDependencyCreator extends AbstractTestDataDependencyCreator {

    public static final String ADMIN_PERSON = "ADMIN_PERSON";

    public static final String PERSON_1 = "PERSON_1";

    public static final String PERSON_2 = "PERSON_2";

    public static final String PERSON_3 = "PERSON_3";

    public static final String PERSON_4 = "PERSON_4";

    public static final String PERSON_5 = "PERSON_5";

    public PersonTestDataDependencyCreator(List<TestDataDependencyCreator> dependencies) {
        super(dependencies);
    }

    public void createDependentTestData(Map<String, Object> context) {
        Person admin = this.createPerson(context);
        this.addAllPrivileges(context, admin);
        
        Person person1 = this.createPerson(context);
        person1.setFirstName(PERSON_1);
        person1.setLastName(PERSON_1);
        
        Person person2 = this.createPerson(context);
        person2.setFirstName(PERSON_2);
        person2.setLastName(PERSON_2);
        
        Person person3 = this.createPerson(context);
        person3.setFirstName(PERSON_3);
        person3.setLastName(PERSON_3);
        
        Person person4 = this.createPerson(context);
        person4.setFirstName(PERSON_4);
        person4.setLastName(PERSON_4);
        
        Person person5 = this.createPerson(context);
        person5.setFirstName(PERSON_5);
        person5.setLastName(PERSON_5);
        
        admin = this.insertDomainObject(admin);
        person1 = this.insertDomainObject(person1);
        person2 = this.insertDomainObject(person2);
        person3 = this.insertDomainObject(person3);
        person4 = this.insertDomainObject(person4);
        person5 = this.insertDomainObject(person5);

        this.storeObjectReferenceInContext(context, ADMIN_PERSON, admin);
        this.storeObjectReferenceInContext(context, PERSON_1, person1);
        this.storeObjectReferenceInContext(context, PERSON_2, person2);
        this.storeObjectReferenceInContext(context, PERSON_3, person3);
        this.storeObjectReferenceInContext(context, PERSON_4, person4);
        this.storeObjectReferenceInContext(context, PERSON_5, person5);
    }

    private void addAllPrivileges(Map<String, Object> context, Person person) {
        Domain hexactaDomain = this.loadObjectFromContext(context, HEXACTA_DOMAIN);
        for (UserPrivilegeType upt : this.loadAllUserPrivilegeType()) {
            UserPrivilege userPrivilege = new UserPrivilege();
            userPrivilege.setPrivilegeType(upt);
            userPrivilege.setPerson(person);
            userPrivilege.setDomain(hexactaDomain);
            userPrivilege.setGlobal(upt.getGlobalRole());
            person.getPrivileges().add(userPrivilege);
        }
    }

    private Person createPerson(Map<String, Object> context) {
        return this.createPerson(context, this.getDataUtils().createPersonName(), this.getDataUtils()
                .createPersonName());
    }

    private Person createPerson(Map<String, Object> context, String name, String surname) {
        Domain hexactaDomain = this.loadObjectFromContext(context, HEXACTA_DOMAIN);
        Locale spanish = this.loadCodedObjectFromContext(context, SPANISH_LOCALE);
        return this.getDataUtils().createPerson(context, hexactaDomain, spanish, name, surname);
    }

    private List<UserPrivilegeType> loadAllUserPrivilegeType() {
        return new HibernateTemplate(this.getSessionFactory()).loadAll(UserPrivilegeType.class);
    }

}
